package com.dycong.common.IO.netty.ssl.server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

import java.util.concurrent.atomic.AtomicBoolean;

/**
 * Created by dycong on 2017/4/5.
 */
public class SecureSocketServer {
    private EventLoopGroup bossGroup = null;
    private EventLoopGroup workerGroup = null;
    private int port = 12121;
    AtomicBoolean isActive = new AtomicBoolean(false);
    ChannelFuture future;

    public void start() {
        bossGroup = new NioEventLoopGroup();
        workerGroup = new NioEventLoopGroup();
        ServerBootstrap serverBootstrap = new ServerBootstrap();
        serverBootstrap.group(bossGroup, workerGroup)
                .channel(NioServerSocketChannel.class)
                .handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new SecureServerInitializer())
        ;

        try {
            future = serverBootstrap.bind(port).sync();

            future.channel().closeFuture().sync();
        } catch (InterruptedException e) {

        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }

    public static void main(String[] args) {
        SecureSocketServer secureSocketServer=new SecureSocketServer();
        secureSocketServer.start();
    }

}
